/*
 * sharedMemory.c
 *
 *  Created on: 29/set/2014
 *      Author: root
 */

#include "sharedMemory.h"

int allocSharedMemory(key_t key, int size){
	int shmId;
	if ((shmId = shmget(key, size, IPC_CREAT | 0666)) < 0) {
		notifyError(ERR_SHMGET,1,0);
	}
	return shmId;
}

void deallocSharedMemory(int shmId){
	if(shmctl(shmId, IPC_RMID, NULL) == -1){
		notifyError(ERR_SHMCTL,1,0);
	}
}

void * attachSharedMemory(int shmId){
	void * pointer;
	if ((pointer = shmat(shmId, NULL, 0)) == (void *) -1) {
		notifyError(ERR_SHMAT,1,0);
	}
	return pointer;
}

void detachSharedMemory(void *pointer){
	if(shmdt(pointer)==-1){
		notifyError(ERR_SHMDT,1,0);
	}
}

